//
//  TestMKUICustomTabBar.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/8/3.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestMKUICustomTabBarItem.h"

@implementation TestMKUICustomTabBarItem
- (id)init{
    if(self=[super init]){
        self.itemCellClass = TestMKUICustomTabBarItemCellViewScrollToTop.class;
    }
    return self;
}
- (void)setSelectedImageOfScrollToTop:(UIImage *)selectedImageOfScrollToTop{
    if(_selectedImageOfScrollToTop==selectedImageOfScrollToTop)return;
    _selectedImageOfScrollToTop = selectedImageOfScrollToTop;
    [self refreshItemCell];
}
- (void)setImageOfScrollToTop:(UIImage *)imageOfScrollToTop{
    if(_imageOfScrollToTop==imageOfScrollToTop)return;
    _imageOfScrollToTop = imageOfScrollToTop;
    [self refreshItemCell];
}
@end


@interface TestMKUICustomTabBarItemCellViewScrollToTop()

@end

@implementation TestMKUICustomTabBarItemCellViewScrollToTop
- (void)customReloadCellModel{
    [super customReloadCellModel];
    TestMKUICustomTabBarItem *tabBarItem = self.customTabBarItem;
    if(tabBarItem.shouldScrollToTop){
        [self.itemButton setImage:tabBarItem.selectedImageOfScrollToTop forState:(UIControlStateSelected)];
        [self.itemButton setImage:tabBarItem.imageOfScrollToTop?:tabBarItem.selectedImageOfScrollToTop forState:(UIControlStateNormal)];
    }else{
        [self.itemButton setImage:tabBarItem.selectedImage forState:(UIControlStateSelected)];
        [self.itemButton setImage:tabBarItem.image forState:(UIControlStateNormal)];
    }
}
- (void)playScrollToTop:(BOOL)shouldScrollToTop animated:(BOOL)animated{
    TestMKUICustomTabBarItem *tabBarItem = self.customTabBarItem;
    BOOL selected = self.collectionCellModel.selected;
    UIImage *selectedImage = shouldScrollToTop?tabBarItem.selectedImageOfScrollToTop:tabBarItem.selectedImage;
    UIImage *image = shouldScrollToTop?tabBarItem.imageOfScrollToTop:tabBarItem.image;
    if(!animated){
        tabBarItem.shouldScrollToTop = shouldScrollToTop;
        [self.itemButton setImage:selectedImage forState:(UIControlStateSelected)];
        [self.itemButton setImage:image?:selectedImage forState:(UIControlStateNormal)];
        return;
    }
    
    CGRect bounds = self.itemButton.imageView.bounds;
    UIView *container = [[UIView alloc] init];
    container.clipsToBounds = YES;
    container.frame = self.itemButton.imageView.frame;
    [self.itemButton.imageView.superview addSubview:container];
    
    UIImageView *fromView = [[UIImageView alloc] init];
    fromView.contentMode = self.itemButton.imageView.contentMode;
    fromView.image = self.itemButton.imageView.image;
    fromView.frame = container.bounds;
    [container addSubview:fromView];
    
    UIImageView *toView = [[UIImageView alloc] init];
    toView.image = selected?selectedImage:image;
    toView.contentMode = fromView.contentMode;
    toView.frame = container.bounds;
    [container addSubview:toView];
    
    self.itemButton.imageView.hidden = YES;
    toView.transform = CGAffineTransformMakeTranslation(0, shouldScrollToTop?bounds.size.height:-bounds.size.height);
    [UIView animateWithDuration:0.25 animations:^{
        toView.transform = CGAffineTransformIdentity;
        fromView.transform = CGAffineTransformMakeTranslation(0, shouldScrollToTop?-bounds.size.height:bounds.size.height);
    } completion:^(BOOL finished) {
        self.itemButton.imageView.hidden = NO;
        [container removeFromSuperview];
        
        tabBarItem.shouldScrollToTop = shouldScrollToTop;
        [self.itemButton setImage:selectedImage forState:(UIControlStateSelected)];
        [self.itemButton setImage:image?:selectedImage forState:(UIControlStateNormal)];
    }];
}
@end


@interface TestScrollViewScrollHandle(){
    CGPoint _preOffset;
}

@end
@implementation TestScrollViewScrollHandle
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    CGFloat position = self.position;
    if(_preOffset.y<=position && offset.y>position){//一屏滚动到二屏
        if(self.whenOverPosition){
            self.whenOverPosition(self,YES);
        }
    }else if(_preOffset.y>=position && offset.y<position){//二屏滚动到一屏
        if(self.whenOverPosition){
            self.whenOverPosition(self,NO);
        }
    }
    _preOffset = offset;
}
@end
